// 导入登录接口
import { login, sysProfile, sysUser } from '@/api/user'

// import { getToken, setToken } from '@/utils/auth'
const state = {
  token: '',
  userInfo: ''
}
const getters = {}
const mutations = {
  setToken(state, value) {
    state.token = value
  },
  setUserInfo(state, value) {
    state.userInfo = value
  },
  logout(state) {
    state.token = ''
    state.userInfo = ''
  }
}
const actions = {
  async toLogin({ commit }, value) {
    const res = await login(value)
    // console.log(res)
    // 将结果保存至token
    commit('setToken', res.data)
  },
  async getUserInfo({ commit }) {
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    // console.log(res)
    // console.log(res2)
    // Object.assign数组/对象合并,会返回一个新数组
    // Object.assign(目标对象,合并对象1,合并对象2)
    commit('setUserInfo', Object.assign({}, res.data, res2.data))
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
